package com.sinoeyes.controller.compent;

import com.sinoeyes.common.constant.Constants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.result.R;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.utils.bean.ObjectUtils;
import com.sinoeyes.compent.po.ProductInfo;
import com.sinoeyes.compent.service.IProductInfoService;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 产品模块
 *
 * @author aries.hao
 */
@Api(value = "Product API", tags = {"产品信息接口"})
@RestController
@Slf4j
@RequestMapping("/product")
public class IProductController extends BaseController {

    private final IProductInfoService productInfoService;

    @Autowired
    public IProductController(IProductInfoService productInfoService) {
        this.productInfoService = productInfoService;
    }

    @ApiOperation("获取单条产品信息")
    @ApiImplicitParam(name = "productId", value = "产品ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getProductInfoById/{productId}")
    public String getDimensionInfoById(@PathVariable String productId) {
        log.info("接收到的查询产品ID：" + productId);
        ProductInfo productInfo = productInfoService.selectProductInfoById(productId);
        if (ObjectUtils.isNotEmpty(productInfo)) {
            return convertJsonWithDate(R.success(productInfo));
        }
        return jsonResult(R.failure("未获取到此产品相关信息"));
    }

    @ApiOperation("获取所有顶级产品分类信息")
    @GetMapping("/getParentProducts")
    public String getParentProducts() {
//        List<ProductInfo> productInfos = productInfoService.selectParentProducts();
//        if (!CollectionUtils.isEmpty(productInfos)) {
//            return convertJsonWithDate(R.success(getRestTable(productInfos)));
//        }
        return jsonResult(R.failure("未获取到顶级产品分类信息！"));
    }

    @ApiOperation("获取顶级产品下的分类信息")
    @GetMapping("/getAllProductByParentId/{parentId}")
    public String getAllProductByParentId(@PathVariable String parentId) {
//        List<ProductInfo> childrenProducts = productInfoService.selectAllChildrenProduct(parentId);
//        if (!CollectionUtils.isEmpty(childrenProducts)) {
//            return convertJsonWithDate(R.success(getRestTable(childrenProducts)));
//        }
        return jsonResult(R.failure("此分类下暂无产品信息"));
    }

    @ApiOperation("条件获取产品信息")
    @PostMapping("/getProductByFilter")
    public String getProductByFilter(@RequestBody(required = false) ProductInfo productInfo) {
//        startPage();
//        List<ProductInfo> productInfoList = productInfoService.selectProductInfoList(productInfo);
//        if (!CollectionUtils.isEmpty(productInfoList)) {
//            return convertJsonWithDate(R.success(getRestTable(productInfoList)));
//        }
        return jsonResult(R.failure("未获取到产品信息！"));
    }

    @ApiOperation("添加产品信息")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addProduct")
    public String addProduct(@RequestBody ProductInfo productInfo) {
        log.info("###########产品信息：" + productInfo.toString());
        int i = productInfoService.insertProductInfo(productInfo);
        if (i > 0) {
            return jsonResult(R.success("产品信息添加成功"));
        }
        return jsonResult(R.failure("产品信息添加失败"));
    }

    @ApiOperation("更新产品信息")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @ApiImplicitParam(name = "productInfo", value = "修改产品信息", dataType = "ProductInfo")
    @PostMapping("/updateProduct")
    public String updateProduct(@RequestBody ProductInfo productInfo) {
        log.info("###########产品信息：" + productInfo.toString());
        int i = productInfoService.updateProductInfo(productInfo);
        if (i > 0) {
            return jsonResult(R.success("产品信息修改成功"));
        }
        return jsonResult(R.failure("产品信息修改失败"));
    }

    @ApiOperation("删除产品信息")
    @Log(title = "产品管理", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name = "productId", value = "产品ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/deleteDimension/{productId}")
    public String deleteDimension(@PathVariable String productId) {
        RCode rCode = productInfoService.deleteProductInfoByIds(productId, Constants.DEL_FLAG);
        if (rCode.getOperateCount() > 0) {
            return jsonResult(R.success("产品信息删除成功"));
        }
        return jsonResult(R.failure("产品信息删除失败[" + rCode.getMessage() + "]"));
    }

    @ApiOperation("启用产品信息")
    @ApiImplicitParam(name = "productId", value = "产品ID", required = true, dataType = "String", paramType = "path")
    @PutMapping("/enableDimension/{productId}")
    public String enableDimension(@PathVariable String productId) {
        RCode rCode = productInfoService.deleteProductInfoByIds(productId, Constants.ENABlE_FLAG);
        if (rCode.getOperateCount() > 0) {
            return jsonResult(R.success("产品信息启用成功"));
        }
        return jsonResult(R.failure("产品信息启用失败[" + rCode.getMessage() + "]"));
    }
}
